package org.fireflow2.designer.eclipse.properties.tab;

import org.eclipse.gef.editparts.AbstractEditPart;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;

//import commonproperties.model.FileModel;

public class EventPropertySection extends AbstractPropertySection {

	ListViewer eventListenerViwer;
	AbstractEditPart editPart = null;
	AbsWrapper modelWrapper = null;

	@Override
	public void createControls(final Composite parent,
			TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
//		datafieldsWrapper.setLayout(new FillLayout());
//		Composite composite = getWidgetFactory() .createFlatFormComposite(datafieldsWrapper);
		
		parent.setLayout(new FormLayout());
		
		Button cancelButton = getWidgetFactory().createButton(parent, "Delete", SWT.PUSH);
		FormData formData = new FormData();
		formData.right = new FormAttachment(60, 0);
		formData.bottom = new FormAttachment(100, -5);
		cancelButton.setLayoutData(formData);
		cancelButton.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				deleteListener();
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				deleteListener();
			}
			
			private void deleteListener(){
				StructuredSelection selection = (StructuredSelection)eventListenerViwer.getSelection();
				
				//modelWrapper.deleteEventListener(selection.toList());
				refresh();
			}
			
		});

		Button okButton = new Button(parent, SWT.PUSH);
		okButton.setText("Add");
		formData = new FormData();
		formData.right = new FormAttachment(cancelButton, -5);
		formData.bottom = new FormAttachment(100, -5);
		okButton.setLayoutData(formData);
		okButton.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				addListener();
			}

			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				addListener();
			}
			
			private void addListener(){
				InputDialog dialog = new InputDialog(parent.getShell(),"Listener Class Name","Input the lisener full class name:","",null);
				dialog.open();
				if (dialog.getReturnCode()==InputDialog.OK){
					String className = dialog.getValue();
					//modelWrapper.addEventListener(className);
					refresh();
				}
			}
			
		});

		
		eventListenerViwer = new ListViewer(parent, SWT.MULTI |SWT.BORDER|SWT.V_SCROLL);

		formData = new FormData();
		formData.top = new FormAttachment(0, 5);
		formData.bottom = new FormAttachment(cancelButton, -5);
		formData.left = new FormAttachment(0, 5);
		formData.right = new FormAttachment(100, -5);
		formData.height = 80;
		eventListenerViwer.getControl().setLayoutData(formData);

		eventListenerViwer.setLabelProvider(new LabelProvider() {
			public String getText(Object element) {
//				EventListener listener = (EventListener) element;
//				return listener.getClassName();
				return "";
			}
		});

		eventListenerViwer.setContentProvider(new ArrayContentProvider());

	}

	@Override
	public void refresh() {
		super.refresh();
//		if (modelWrapper instanceof TaskRefWrapper){
//			List eventList = ((TaskRefWrapper)modelWrapper).getEventListeners();
//			eventListenerViwer.setInput(eventList);
//		}
//		else {
//			if (modelWrapper.getWorkflowModelElement() != null) {
//
//				List eventList = modelWrapper.getWorkflowModelElement().getEventListeners();
//
//				eventListenerViwer.setInput(eventList);
//			}
//		}
	}

	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Object obj = ((IStructuredSelection) selection).getFirstElement();
		if (obj instanceof AbstractEditPart) {
			editPart = (AbstractEditPart) obj;
			modelWrapper = (AbsWrapper) editPart.getModel();
		}
	}

}
